//
//  ZQSBaseVC.m
//  mylike
//
//  Created by Mylike on 2019/9/9.
//  Copyright © 2019 Mylike. All rights reserved.
//

#import "ZQSBaseVC.h"

//网络监测第三方
//#import "RealReachability.h"
//#import "EmptyView.h"

#import "UIColor+HEX.h"
#import "Color.h"

@interface ZQSBaseVC ()





@end

@implementation ZQSBaseVC

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.view.backgroundColor = [UIColor whiteColor];
    
    self.navigationController.interactivePopGestureRecognizer.delegate = (id)self;
    self.emptyV.hidden = YES;
//    RealReachability *reachability = [RealReachability sharedInstance];
//    ReachabilityStatus status = [reachability currentReachabilityStatus];
//    switch (status) {
//        case 0:
//        {
//            _netStatus = XSWNetWorkStatusNoInternet;
//        }
//            break;
//        case 1:{
//            _netStatus = XSWNetWorkStatusFlow;
//        }
//            break;
//        case 2:{
//            _netStatus = XSWNetWorkStatusWifi;
//        }
//            break;
//        default:
//            break;
//    }
//    //监听网络状态
//    [[NSNotificationCenter defaultCenter] addObserver:self
//                                             selector:@selector(networkChanged:)
//                                                 name:kRealReachabilityChangedNotification
//                                               object:nil];
}



- (void)initNavgationControllerWithBGColor:(NSString *)colorStr navTitle:(NSString*)title isLeft:(BOOL)isLeft rightBtnWithTitle:(NSString*)tit isImage:(BOOL)isImage {
    [self setNeedsStatusBarAppearanceUpdate];
    [self.bgView addSubview:self.contentBgView];
//    self.bgView.backgroundColor = [Color colorWithHexString:colorStr];
    if (![colorStr isEqualToString:@"#ffffff"]) {
        self.contentBgView.image = [UIImage imageNamed:@"back_blue.png"];
    }
    [self.view addSubview:self.bgView];
    
    UIButton *leftBarBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    leftBarBtn.frame = CGRectMake(10, (44-30)/2, 30, 30);
    [leftBarBtn addTarget:self action:@selector(popClick) forControlEvents:UIControlEventTouchUpInside];
    //    [leftBarBtn setBackgroundImage: forState:UIControlStateNormal];
    [leftBarBtn setImage:[colorStr isEqualToString:@"ffffff"]?[UIImage imageNamed:@"z1"]:[UIImage imageNamed:@"zuo"] forState:UIControlStateNormal];
    [[leftBarBtn imageView] setContentMode:UIViewContentModeScaleAspectFit];
    leftBarBtn.adjustsImageWhenHighlighted = NO;
    if (isLeft) {
        [self.bgView addSubview:leftBarBtn];
    }
    
    UILabel *_navBarTitle = [[UILabel alloc] initWithFrame:CGRectMake(UIScreen.mainScreen.bounds.size.width/2 - 60, 0 + (44-30)/2, 120, 30)];
    _navBarTitle.textColor = [colorStr isEqualToString:@"ffffff"]?[Color colorWithHexString:@"000000"]:[Color colorWithHexString:@"ffffff"];
    _navBarTitle.font = [UIFont boldSystemFontOfSize:16];
    _navBarTitle.text = title;
    _navBarTitle.textAlignment = NSTextAlignmentCenter;
    [self.bgView addSubview:_navBarTitle];
    self.navTitleLbl = _navBarTitle;
    
    UIButton *_rightBarBuBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    
    [_rightBarBuBtn addTarget:self action:@selector(rightBarBtnClick) forControlEvents:UIControlEventTouchUpInside];
    [_rightBarBuBtn setTitleColor:[Color colorWithHexString:@"666666"] forState:UIControlStateNormal];
    
    _rightBarBuBtn.titleLabel.font = [UIFont systemFontOfSize:15];
    if (tit.length > 0 ) {
        if (isImage) {
            _rightBarBuBtn.frame = CGRectMake(UIScreen.mainScreen.bounds.size.width - 35, 0 + (44-22)/2, 22, 22);
            [_rightBarBuBtn setImage:[UIImage imageNamed:tit] forState:UIControlStateNormal];
            [self.bgView addSubview:_rightBarBuBtn];
        }else{
            _rightBarBuBtn.frame = CGRectMake(UIScreen.mainScreen.bounds.size.width - 80, 0 + (44-30)/2, 70, 30);
            [_rightBarBuBtn setTitle:tit forState:UIControlStateNormal];
            _rightBarBuBtn.contentHorizontalAlignment = UIControlContentHorizontalAlignmentRight;
            [self.bgView addSubview:_rightBarBuBtn];
        }
    }
    self.rightBarBuBtn = _rightBarBuBtn;
    
//    UIView * navBotLine = [[UIView alloc]initWithFrame:CGRectMake(0, self.bgView.height_ml - 1, UIScreen.mainScreen.bounds.size.width, 1)];
//    navBotLine.backgroundColor = [Color colorWithHexString:@"f1f1f1"];
//    [self.bgView addSubview:navBotLine];
}

//导航栏 右侧按钮颜色
- (void)initNavgationControllerWithBGColor:(NSString *)colorStr navTitle:(NSString*)title isLeft:(BOOL)isLeft rightBtnWithTitle:(NSString*)tit rightTextColor:(NSString *)rColor {
    [self setNeedsStatusBarAppearanceUpdate];
    [self.bgView addSubview:self.contentBgView];
//    self.bgView.backgroundColor = [Color colorWithHexString:colorStr];
    if (![colorStr isEqualToString:@"#ffffff"]) {
        self.contentBgView.image = [UIImage imageNamed:@"back_blue.png"];
    }
    [self.view addSubview:self.bgView];
    
    UIButton *leftBarBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    leftBarBtn.frame = CGRectMake(10, 0 + (44-30)/2, 30, 30);
    [leftBarBtn addTarget:self action:@selector(popClick) forControlEvents:UIControlEventTouchUpInside];
    //    [leftBarBtn setBackgroundImage: forState:UIControlStateNormal];
    [leftBarBtn setImage:[colorStr isEqualToString:@"ffffff"]?[UIImage imageNamed:@"返回_icon"]:[UIImage imageNamed:@"返回_icon"] forState:UIControlStateNormal];
    [[leftBarBtn imageView] setContentMode:UIViewContentModeScaleAspectFit];
    leftBarBtn.adjustsImageWhenHighlighted = NO;
    if (isLeft) {
        [self.bgView addSubview:leftBarBtn];
    }
    self.leftBarBuBtn = leftBarBtn;
    
    UILabel *_navBarTitle = [[UILabel alloc] initWithFrame:CGRectMake(UIScreen.mainScreen.bounds.size.width/2 - 60, 0 + (44-30)/2, 120, 30)];
    _navBarTitle.textColor = [colorStr isEqualToString:@"ffffff"]?[Color colorWithHexString:@"000000"]:[Color colorWithHexString:@"000000"];
    _navBarTitle.font = [UIFont boldSystemFontOfSize:16];
    _navBarTitle.text = title;
    _navBarTitle.textAlignment = NSTextAlignmentCenter;
    [self.bgView addSubview:_navBarTitle];
    self.navTitleLbl = _navBarTitle;
    
    UIButton *_rightBarBuBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    
    [_rightBarBuBtn addTarget:self action:@selector(rightBarBtnClick) forControlEvents:UIControlEventTouchUpInside];
    [_rightBarBuBtn setTitleColor:[Color colorWithHexString:rColor] forState:UIControlStateNormal];
    _rightBarBuBtn.titleLabel.font = [UIFont systemFontOfSize:15];
    if (tit.length > 0 ) {
        [_rightBarBuBtn setTitle:tit forState:UIControlStateNormal];
        
    }
    _rightBarBuBtn.contentHorizontalAlignment = UIControlContentHorizontalAlignmentRight;
    _rightBarBuBtn.frame = CGRectMake(UIScreen.mainScreen.bounds.size.width - 80, 0 + (44-30)/2, 70, 30);
    [self.bgView addSubview:_rightBarBuBtn];
    self.rightBarBuBtn = _rightBarBuBtn;
    
//    UIView * navBotLine = [[UIView alloc]initWithFrame:CGRectMake(0, self.bgView.height_ml - 1, UIScreen.mainScreen.bounds.size.width, 1)];
//    navBotLine.backgroundColor = [Color colorWithHexString:@"f1f1f1"];
//    [self.bgView addSubview:navBotLine];
}
-(void)changeNavBarColor{
//    dispatch_async(dispatch_get_main_queue(), ^{
//        self.bgView.backgroundColor = [Color colorWithHexString:@"FFAA61"];
//    });
}

-(void)changeNavColorBlue:(BOOL)isBlue {
    dispatch_async(dispatch_get_main_queue(), ^{
        if (isBlue) {
            self.contentBgView.image = [UIImage imageNamed:@"back_blue.png"];
        }else{
            self.contentBgView.image = [UIImage imageNamed:@"back_orange.png"];
        }
    });
}

- (void)popClick {
    [self.navigationController popViewControllerAnimated:YES];
}

- (void)rightBarBtnClick {
    
}

- (void)showMessage:(NSString *)message {
    // 获取window
    UIWindow *window = [UIApplication sharedApplication].keyWindow;
    UIView *showView = [[UIView alloc] init];
    showView.backgroundColor = [UIColor blackColor];
    showView.frame = CGRectMake(1, 1, 1, 1);
    showView.alpha = 1.0f;
    showView.center = window.center;
    showView.layer.cornerRadius = 5.0f;
    showView.layer.masksToBounds = YES;
    [window addSubview:showView];
    
    UILabel *label = [[UILabel alloc] init];
    UIFont *font = [UIFont systemFontOfSize:16];
    CGRect labelRect = [message boundingRectWithSize:CGSizeMake(CGFLOAT_MAX, 30) options:NSStringDrawingUsesLineFragmentOrigin attributes:@{NSFontAttributeName: font} context:nil];
    
    label.text = message;
    label.textColor = [UIColor whiteColor];
    label.font = [UIFont systemFontOfSize:16];
    label.textAlignment = NSTextAlignmentCenter;
    label.backgroundColor = [UIColor clearColor];
    [showView addSubview:label];
    showView.frame = CGRectMake((UIScreen.mainScreen.bounds.size.width - CGRectGetWidth(labelRect) - 20)/2, UIScreen.mainScreen.bounds.size.height/2 - (CGRectGetHeight(labelRect)+20)/2, CGRectGetWidth(labelRect)+20, CGRectGetHeight(labelRect)+20);
    
    
    label.frame = CGRectMake(10, 0, ceil(CGRectGetWidth(labelRect)), CGRectGetHeight(labelRect)+20);
    
    [UIView animateWithDuration:2.5 animations:^{
        showView.alpha = 0;
    } completion:^(BOOL finished) {
        [showView removeFromSuperview];
    }];
    
}
- (void)zqsLoadView:(NSString *)loadStr {
//    [MBProgressHUD showInWindowActivityWithMessage:loadStr];
}

- (void)dismissLoadView {
//    [SVProgressHUD dismiss];
//    [MBProgressHUD hideHUD];
}
- (void)loadSvpView:(NSString *)loadStr {
//    [MBProgressHUD hideHUD];
//    [MBProgressHUD showInWindowMessage:loadStr];
//     [MBProgressHUD showErrorWithImg:@"账号不能为空" :[UIImage imageNamed:@"errormark"] toView:self.view];
}


#pragma mark ==================监听事件的回调==================
- (void)networkChanged:(NSNotification *)notification
{
//    RealReachability *reachability = (RealReachability *)notification.object;
//    ReachabilityStatus status = [reachability currentReachabilityStatus];
//    switch (status) {
//        case 0:
//        {
//            _netStatus = XSWNetWorkStatusNoInternet;
//            [self monitorNetStateChanged:0];
//        }
//            break;
//        case 1:{
//
//            _netStatus = XSWNetWorkStatusFlow;
//            [self monitorNetStateChanged:1];
//        }
//            break;
//        case 2:{
//
//            _netStatus = XSWNetWorkStatusWifi;
//            [self monitorNetStateChanged:2];
//        }
//            break;
//        default:
//            break;
//    }
}
//添加没有网络界面
- (void)addEmptyViewWithFrame:(CGRect)frame {
    
//    EmptyView *emptyView = [[EmptyView alloc] initWithFrame:frame];
//    emptyView.delegates = self;
//    [self.view addSubview:emptyView];
}
//无网络页面的回调事件
- (void)clickEmptyViewWithAction:(UIView *)sender {
    
    [self clickEmptyView];
}
-(void)clickEmptyView {
    
    //子类实现
}
//移除没有网络界面
- (void)removeEmptyView {
//    
//    for (UIView *view in self.view.subviews) {
//        
//        if ([view isKindOfClass:[EmptyView class]]) {
//            
//            [view removeFromSuperview];
//        }
//    }
}
#pragma mark ==================网络状态改变==================
-(void)monitorNetStateChanged:(NSInteger)netState {
    
    //子类实现
}
#pragma mark ==================移除通知==================
-(void)dealloc {
    
    [[NSNotificationCenter defaultCenter] removeObserver:self];
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}


- (void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    [self.navigationController setNavigationBarHidden:YES animated:NO];
}

- (void)viewWillDisappear:(BOOL)animated {
    [super viewWillDisappear:animated];
    [self.navigationController setNavigationBarHidden:NO animated:NO];
    
}
-(void)viewDidAppear:(BOOL)animated
{
    [super viewDidAppear:animated];
    if (self.navigationController.viewControllers.count > 1) {
        self.navigationController.interactivePopGestureRecognizer.enabled = YES;
    }else{
        self.navigationController.interactivePopGestureRecognizer.enabled = NO;
    }
}

-(UIImageView *)bgView{
    if (!_bgView) {
        self.bgView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, UIScreen.mainScreen.bounds.size.width, 0+44)];
        self.bgView.backgroundColor = [Color colorWithHexString:@"#F7F7F7"];
        _bgView.userInteractionEnabled = true;
    }
    return _bgView;
}


- (UIImageView *)contentBgView {
    if (!_contentBgView) {
        // 750 900
        _contentBgView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, UIScreen.mainScreen.bounds.size.width, UIScreen.mainScreen.bounds.size.width*900/750.0)];
        _contentBgView.contentMode = UIViewContentModeScaleToFill;
    }
    return _contentBgView;
}

- (EmptyView *)emptyV {
    if (!_emptyV) {
        _emptyV = [EmptyView new];
        [self.view addSubview:_emptyV];
        _emptyV.frame = CGRectMake(0, 0 + 44, UIScreen.mainScreen.bounds.size.width, UIScreen.mainScreen.bounds.size.height-0-44);
    }
    return _emptyV;
}

@end
